package com.swu.pad.assignment.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.swu.pad.assignment.entity.Assignment;
import com.swu.pad.assignment.entity.AssignmentPad;
import com.swu.pad.assignment.service.AssignmentService;
import com.swu.pad.course.entity.CourseGroupUser;
import com.swu.pad.system.entity.User;
import com.swu.pad.util.CookieUtil;

@Controller
@RequestMapping(value = "/assignment")
public class AssignmentController {

	@Autowired
	private AssignmentService assignmentService;

	@ResponseBody
	@RequestMapping(value = "/createAssignment")
	public String createAssignment(Assignment assignment) {
		assignmentService.addAssignment(assignment);
		return "success";
	}
	@ResponseBody
	@RequestMapping(value = "/updateAssignment")
	public String updateAssignment(Assignment assignment) {
		assignmentService.updateAssignment(assignment);
		return "success";
	}

	@ResponseBody
	@RequestMapping(value = "/deleteAssignment")
	public String deleteAssignment(String assignmentId) {
		assignmentService.deleteAssignment(assignmentId);
		return "success";
	}

	@ResponseBody
	@RequestMapping(value = "/getAssignments")
	public List<Assignment> getAssignments(String courseId) {
		return assignmentService.getAssignmentByCourseId(courseId);
	}

	@ResponseBody
	@RequestMapping(value = "/getAssignment")
	public Assignment getAssignment(String assignmentId) {
		return assignmentService.getAssignment(assignmentId);
	}

	@ResponseBody
	@RequestMapping(value = "/getAssignmentDetail")
	public Map<String, Object> getAssignmentDetail(String assignmentId) {
		return assignmentService.getAssignmentDetail(assignmentId);
	}

	@ResponseBody
	@RequestMapping(value = "/getAssignmentPadId")
	public AssignmentPad getAssignmentPadId(String assignmentId, HttpSession session, HttpServletRequest request, HttpServletResponse response) {
		User user = (User) session.getAttribute("user");
		if (User.ROLE_STUDENT.equals(user.getRole())) {
			CourseGroupUser courseGroupUser = assignmentService.getUserAssignmentSession(assignmentId, user.getId());
			Cookie cookie = CookieUtil.getCookieByName(request, "sessionID");
			if (null == cookie) {
				cookie = new Cookie("sessionID", courseGroupUser.getId());
				cookie.setPath("/");
				cookie.setMaxAge(60 * 60);// 设置为60min
				response.addCookie(cookie);
			} else {
				cookie.setValue(courseGroupUser.getId());
				cookie.setPath("/");
				cookie.setMaxAge(60 * 60);// 设置为30min
				response.addCookie(cookie);
			}
			return assignmentService.getUserAssignmentPad(assignmentId, courseGroupUser.getGroupId());
		} else {
			return null;
		}
	}

	@ResponseBody
	@RequestMapping(value = "/getAssignmentPadCurrentId")
	public Map<String, Object> getAssignmentPadCurrentId(String assignmentId, HttpSession session, HttpServletRequest request, HttpServletResponse response) {
		User user = (User) session.getAttribute("user");
		if (User.ROLE_STUDENT.equals(user.getRole())) {
			CourseGroupUser courseGroupUser = assignmentService.getUserAssignmentSession(assignmentId, user.getId());
			Cookie cookie = CookieUtil.getCookieByName(request, "sessionID");
			if (null == cookie) {
				cookie = new Cookie("sessionID", courseGroupUser.getId());
				cookie.setPath("/");
				cookie.setMaxAge(60 * 60);// 设置为60min
				response.addCookie(cookie);
			} else {
				cookie.setValue(courseGroupUser.getId());
				cookie.setPath("/");
				cookie.setMaxAge(60 * 60);// 设置为30min
				response.addCookie(cookie);
			}
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("id", assignmentService.getAssignmentPadCurrentId(assignmentId, courseGroupUser.getGroupId()));
			return map;
		} else {
			return null;
		}
	}

	@ResponseBody
	@RequestMapping(value = "/getCommentAssignmentPad")
	public Map<String, Object> getCommentAssignmentPad(String activityId, HttpSession session, HttpServletRequest request, HttpServletResponse response) {
		User user = (User) session.getAttribute("user");
		if (User.ROLE_STUDENT.equals(user.getRole())) {
			return assignmentService.getCommentAssignmentPad(activityId, user.getId());
		} else {
			return null;
		}
	}
}
